package org.jeecg.modules.wlhy.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerConfig;

import org.jeecg.modules.wlhy.customer.mapper.HyCustomerConfigMapper;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerConfigService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;

/**
 * @Description: hy_customer_config
 * @Author: jeecg-boot
 * @Date:   2021-04-25
 * @Version: V1.0
 */
@Service
public class HyCustomerConfigServiceImpl extends ServiceImpl<HyCustomerConfigMapper, HyCustomerConfig> implements IHyCustomerConfigService {


    @Override
    @Cacheable(unless="#result == null", cacheNames= CacheConstant.CACHE_CUSTOMER_CONFIG, key="#customerId")
    public HyCustomerConfig getByCustomerId(String customerId) {
        QueryWrapper<HyCustomerConfig>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id",customerId);
        HyCustomerConfig hyCustomerConfig = this.getOne(queryWrapper);
        // 没有时创建一条配置
        if(hyCustomerConfig == null) {
            hyCustomerConfig = new HyCustomerConfig();
            hyCustomerConfig.setCustomerId(customerId);
            hyCustomerConfig.setIzDriverMl("0");//司机结算是否抹零
            hyCustomerConfig.setDriverMlWs("10");
            hyCustomerConfig.setIzDriverPayset("0");//司机结算
            hyCustomerConfig.setIzCarrierMl("0");//车队长结算是否抹零
            hyCustomerConfig.setCarrierMlWs("10");
            hyCustomerConfig.setIzCarrierPayset("0");//车队长结算
            hyCustomerConfig.setIzCarrierLoss("0");//车队长是否扣除亏吨
            hyCustomerConfig.setIzOpenRangeLimit("0");//是否开启装货范围限制
            hyCustomerConfig.setRangeValue("10");//装货范围值
            hyCustomerConfig.setIzZd("0");
            hyCustomerConfig.setZdJs(BigDecimal.ZERO);
            hyCustomerConfig.setIzCjCustomerLoss("1");
            hyCustomerConfig.setXiaoHundred(0);
            hyCustomerConfig.setDengHundred(0);
            hyCustomerConfig.setDaHundred(0);
            this.save(hyCustomerConfig);
        }
        return hyCustomerConfig;
    }

    @Override
    @CacheEvict(cacheNames= CacheConstant.CACHE_CUSTOMER_CONFIG, key="#hyCustomerConfig.customerId")
    public void edit(HyCustomerConfig hyCustomerConfig) {
        this.updateById(hyCustomerConfig);
    }

    @Override
    public HyCustomerConfig getByCId(String customerId) {
        QueryWrapper<HyCustomerConfig>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id",customerId);
        HyCustomerConfig hyCustomerConfig = this.getOne(queryWrapper);
        return hyCustomerConfig;
    }
}
